package com.ibm.cloud.banking.prod.facade;

import com.ibm.cloud.banking.platform.context.service.RequestContextHandler;
import com.ibm.cloud.banking.prod.convert.ProductInfoMapper;
import com.ibm.cloud.banking.prod.dto.ProductCheckDTO;
import com.ibm.cloud.banking.prod.service.IProductParameterQueryService;

import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;

import lombok.SneakyThrows;

/**
 * replace desc in here
 *
 * @author HanDong
 * @date 2022/7/18
 */
@Component
public class ProdSaleFacadeImpl implements IProdSaleFacade {

    @Resource
    private IProductParameterQueryService productParameterQueryService;

    @Resource
    private ProductInfoMapper productInfoMapper;

    @Override
    @SneakyThrows
    public void checkProd(Object data) {

        ProductCheckDTO productCheckDTO = productInfoMapper.mapToDto((Map)data);

        Date transDate = new SimpleDateFormat("yyyyMMdd").parse(RequestContextHandler.getContext().getBnkGlDate());
        productParameterQueryService.checkProductAvailable(productCheckDTO.getProductNo(), transDate);
        productParameterQueryService.checkAcctProduct(
                productCheckDTO.getProductNo(),
                new BigDecimal(productCheckDTO.getAmount()),
                String.valueOf(productCheckDTO.getType().intValue()));

    }
}
